<?php
namespace App\Http\Controllers\Admin;

use App\Models\Shop;
use App\Models\Statistic;

class StatisticController extends BaseController
{
    public function __construct(Statistic $statistic, Shop $shop)
    {
        parent::__construct();
        $this->statistic = $statistic;
        $this->shop = $shop;
        $this->ctr = 'statistic';
    }

    public function getIndex()
    {
        $type = request('type', 'money');
        if(is_administrator()){
            $where = [];
        } else {
            if(session('shop_info')){
                $where['id'] = session('shop_info.id');
            } else {
                $where['admin_id'] = plat_admin_id();
            }
        }
        $shops = $this->shop->_getList($where,'id,shop_name')->toArray();

        switch ($type){
            case 'delivery':
                // 配送统计
                $shopGoods = $this->statistic->goodsDeliveryStatistic();//商品统计
                $payType = $this->statistic->payTypeDeliveryStatistic();// 支付方式统计
                $worker = $this->statistic->workerDeliveryStatistic();// 送水工统计
                $subView = 'admin.statistic.delivery';
                $data = compact('type', 'shops', 'subView', 'shopGoods', 'payType', 'worker');
                break;
            case 'bill':
                // 水票统计
                $bill = $this->statistic->waterBillStatistic();
                $subView = 'admin.statistic.bill';
                $data = compact('type', 'shops', 'subView', 'bill');
                break;
            case 'bucket':
                // 水桶统计
                $bucket = $this->statistic->bucketDeliveryStatistic();
                $subView = 'admin.statistic.bucket';
                $data = compact('type', 'shops', 'subView', 'bucket');
                break;
            case 'deposit':
                // 押金统计
                $deposit = $this->statistic->depositStatistic();
                $subView = 'admin.statistic.deposit';
                $data = compact('type', 'shops', 'subView', 'deposit');
                break;
            default:
                // 营收统计
                $revenue = $this->statistic->revenueStatistic(); //水票/非水票 统计
                $money = $this->statistic->moneyStatistic(); //款项支付方式/分类 统计
                $goods = $this->statistic->goodsStatistic(); //水票款/订单款 细分商品 统计
                if(!$revenue || !$money || !$goods){
                    return $this->error($this->statistic->getError());
                }
                $subView = 'admin.statistic.money'; // 视图文件
                $data = compact('type', 'shops', 'subView','revenue', 'money', 'goods');
                break;
        }
        $data['ctr'] = $this->ctr;
        $data['action'] = 'getIndex';
        return view('admin.statistic.index', $data);
    }
}